<?php
/**
 * CK框架 实例化的DB代理类。取代了第二版的静态代理
 * @author wujibing<283109896@qq.com>
 * @version   3.0
 * @category   CkFrameWork
 * @package  CkFrameWork
 * @subpackage lib
 */
class dbProxy2
{
    /**
     * 当前的数据库连接对象
     * @var IDb_db
     */
    private $_nowDbObject;
    /**
     * 所有连接的DB对象数组
     * @var array
     */
    private static $_dbObejctArray = array();
    /**
     * 连接的DB表名前缀
     * @var Array
     */
    private static $_dbPrefix = array();
    /**
     * 上级的模块对象
     * @var module
     */
    private $_module;
    /**
     * 初始化
     */
    public function __construct(module $module,$dbIndex)
    {
        $this->_module = $module;
        $this->connect($dbIndex);
    }
    /**
     * 连接或者初始化一个数据库对象
     * @param int $dbIndex 配置文件中的DB索引
     * @throws base_Exception
     */
    public function connect($dbIndex)
    {
        $dbIndex = (int)$dbIndex;
        //判断连接的数据库对象有没存在 存在则不连接
        if(isset(dbProxy2::$_dbObejctArray[$dbIndex]))
        {
            $this->_nowDbObject = dbProxy2::$_dbObejctArray[$dbIndex];
            $this->_module->setPrefixTableName(dbProxy2::$_dbPrefix[$dbIndex]);
            return;
        }
        if($dbIndex < 0 || !isset(config::$configObject->DB[$dbIndex]) || !config::$configObject->DB[$dbIndex] instanceof  stdClass)
        {
            throw new base_Exception(lang::getLang('__CONNECT_DB_INDEX_NOT_FOUND__',array($dbIndex)));
        }
        $dbConfiogObject = config::$configObject->DB[$dbIndex];
        $dbDriverName = $dbConfiogObject->DB_DRIVER . '_db';
        $dbDriver = new $dbDriverName($dbConfiogObject);
        if (!($dbDriver instanceof IDb_db))
        {
            throw new base_Exception(lang::getLang('__DB_DRIVER_NOT_INSTANCEOF_I_DB__',array($dbIndex)));
        }
        $this->_module->setPrefixTableName($dbConfiogObject->PREFIX);
        dbProxy2::$_dbObejctArray[$dbIndex] = $dbDriver;
        $this->_nowDbObject = $dbDriver;
        dbProxy2::$_dbPrefix[$dbIndex] = $dbConfiogObject->PREFIX;
    }
    
    /**
     * 提供直接的SQL语句直接查询
     * @param string $sql
     */
    public function sqlQuery ($sql,$key='')
    {
        return $this->_nowDbObject->query($sql,$key);
    }
    
    /**
     * 提供复杂的SQL语句直接插入
     * @param string $sql
     */
    public function sqlInsert ($sql)
    {
        return $this->_nowDbObject->insert($sql);
    }
    
    /**
     * 提供复杂的SQL语句直接更新
     * @param string $sql
     */
    public function sqlUpdate ($sql)
    {
        return $this->_nowDbObject->update($sql);
    }
    
    /**
     * 提供复杂的SQL语句直接删除
     * @param string $sql
     */
    public function sqlDelete ($sql)
    {
        return $this->_nowDbObject->delete($sql);
    }
    /**
     * 返回上一步 INSERT 操作产生的 ID。
     * @return
     */
    public function getInsertId()
    {
        return $this->_nowDbObject->getInsertId();
    }
    /**
     * 动态查询
     */
    public function query ($key='')
    {
        $sql = $this->getSql( 'query');
        return $this->_nowDbObject->query($sql,$key);
    }
    /**
     * 插入操作
     * @return int
     */
    public function insert ()
    {
        $sql = $this->getSql( 'insert');
        return $this->_nowDbObject->insert($sql);
    }
    /**
     * 开启事物
     */
    public function beginTransaction()
    {
        return $this->_nowDbObject->beginTransaction();
    }
    /**
     * 提交事物
     */
    public function commit()
    {
        return $this->_nowDbObject->commit();
    }
    /**
     * 事物回滚
     */
    public function rollBack()
    {
        return $this->_nowDbObject->rollBack();
    }
    /**
     * 获取查询量
     */
    public function count()
    {
        $sql = $this->getSql('count');
        return $this->_nowDbObject->count($sql);
    }
    /**
     * 更新操作
     */
    public function update ()
    {
        $sql = $this->getSql('update');
        return $this->_nowDbObject->update($sql);
    }
    /**
     * 获取表的字段类型
     */
    public function getFields($tableName)
    {
        return $this->_nowDbObject->getFields($tableName);
    }
    /**
     * 删除操作
     */
    public function delete ()
    {
        $sql = $this->getSql('delete');
        return $this->_nowDbObject->delete($sql);
    }
    /**
     * 获取SQL的操作
     * @param string $method
     */
    private function getSql ($method)
    {
        $dbType = $this->_nowDbObject->getDbType();
        return call_user_func(array($dbType . '_Resolve', $method), $this->_module);
    }
}
